package com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.*;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : RelationInfoTask
 * @Description : 发起方和落地方信息
 * @Author : wyn
 * @Date : 2021/7/13 16:24
 */
public class RelationInfoTask extends AbstractCustViewTask {
    private final ZLogger LOGGER = ZLoggerFactory.getLogger(this.getClass(), KeyConsts.LOG_MODULE);
    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override
    public CustViewBaseVO call(Object[] args) {
        if (args.length != 2) {
            return null;
        }
        // 用户信息查询服务
        Map<String, Object> userDetail = (Map<String, Object>) args[0];
        if (KiteMapUtils.isEmpty(userDetail)) {
            return null;
        }
        CustViewVO custViewVO = (CustViewVO) args[1];
        RelationInfoVO relationInfoVO = new RelationInfoVO();
        custViewVO.setRelation(relationInfoVO);
        List<RelationInfo> relationInfoList = new ArrayList<>();
        if (KiteMapUtils.isNotEmpty(userDetail)) {
            List<Map<String, Object>> userRelationList = (List<Map<String, Object>>) userDetail.get("USER_RELATION_P_INFOS");
            if (KiteListUtils.isNotEmpty(userRelationList)) {
                userRelationList.forEach(userRelation -> {
                    relationInfoList.add(getResultRelationInfo(userRelation));
                });
                relationInfoVO.setRelations(relationInfoList);
            }
        }

        return relationInfoVO;
    }

    /**
     * 得到返回前端的展示信息
     * @param userRelation
     * @return
     */
    private RelationInfo getResultRelationInfo(Map<String, Object> userRelation){
        RelationInfo relationInfo = new RelationInfo();
        String userIdA = KiteMapUtils.getString(userRelation, "USER_ID_A", "");
        String serialNumberA = KiteMapUtils.getString(userRelation, "SERIAL_NUMBER_A", "");
        String userProCodeA = KiteMapUtils.getString(userRelation, "USER_A_PROVINCE_CODE", "");
        String userAreaCodeA = KiteMapUtils.getString(userRelation, "USER_A_EPARCHY_CODE", "");
        String userOneIdA = KiteMapUtils.getString(userRelation, "USER_A_ONE_ID", "");
        String userIdB = KiteMapUtils.getString(userRelation, "USER_ID_B", "");
        String serialNumberB = KiteMapUtils.getString(userRelation, "SERIAL_NUMBER_B", "");
        String userProCodeB = KiteMapUtils.getString(userRelation, "USER_B_PROVINCE_CODE", "");
        String userAreaCodeB = KiteMapUtils.getString(userRelation, "USER_B_EPARCHY_CODE", "");
        String roleCodeB = KiteMapUtils.getString(userRelation, "USER_B_ROLE", "");
        String startDate = KiteMapUtils.getString(userRelation, "START_DATE", "");
        String endDate = KiteMapUtils.getString(userRelation, "END_DATE", "");
        String userACity = "";
        String userBCity = "";
        String roleDescB = "";
        if ("0".equals(roleCodeB)) {
            roleDescB = "A(CE)端落地";
        }
        else if ("1".equals(roleCodeB)) {
            roleDescB = "Z(PE)端落地";
        }
        else {
            roleDescB = "第三方收费";
        }
        userACity = String
            .join(SymbolConsts.HYPHEN, LocalAttrHelper.getRegionName(userProCodeA),
                LocalAttrHelper.getRegionName(userAreaCodeA));
        userBCity = String
            .join(SymbolConsts.HYPHEN, LocalAttrHelper.getRegionName(userProCodeB),
                LocalAttrHelper.getRegionName(userAreaCodeB));
        if (SymbolConsts.HYPHEN.equals(userACity)) {
            userACity = "";
        }
        if (SymbolConsts.HYPHEN.equals(userBCity)) {
            userBCity = "";
        }
        Date start = null;
        Date end = null;
        try {
            start = KiteStringUtils.isNotEmpty(startDate) ? simpleDateFormat.parse(startDate) : null;
            end = KiteStringUtils.isNotEmpty(endDate) ? simpleDateFormat.parse(endDate) : null;
        } catch (ParseException e) {
            LOGGER.error(ErrorConsts.BUSI_COMMON_ERROR.getErrorCode(), e, "格式化时间失败");
        }
        relationInfo.setStartDate(Objects.nonNull(start) ? KiteDateUtils.formatDate(start, KiteDateUtils.DATE_TIME_FORMAT) : "");
        relationInfo.setEndDate(Objects.nonNull(end) ? KiteDateUtils.formatDate(end, KiteDateUtils.DATE_TIME_FORMAT) : "");
        relationInfo.setUserIdA(userIdA);
        relationInfo.setSerialNumberA(serialNumberA);
        relationInfo.setUserACity(userACity);
        relationInfo.setUserAOneId(userOneIdA);
        relationInfo.setUserIdB(userIdB);
        relationInfo.setSerialNumberB(serialNumberB);
        relationInfo.setUserBCity(userBCity);
        relationInfo.setUserBRole(roleCodeB);
        relationInfo.setUserBRoleName(roleDescB);
        relationInfo.setUserAProvinceCode(userProCodeA);
        relationInfo.setUserAEparchyCode(userAreaCodeA);
        relationInfo.setUserBProvinceCode(userProCodeB);
        relationInfo.setUserBEparchyCode(userAreaCodeB);

        return relationInfo;
    }

}
